package com.itheima.service.impl;

import com.itheima.dao.EmployeeDao;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<Employee> findAll() {
        return employeeDao.findAll();
    }

    @Override
    public List<Employee> findByCondition(Integer empId, String name, String sex, Integer deptId) {
        //当表单中不存在某个field的时候，就是null，如果有这个field，但是没输入任何值，就是空字符串
        //因此，当我们不输入姓名或者年龄时，表单提交的name为空字符串，导致查询不到数据
        if("".equals(name)) {
            name = null;
        }
        if("".equals(sex)) {
            sex = null;
        }
        if("".equals(deptId)) {
            deptId = null;
        }
        return employeeDao.findByCondition(new Employee(empId,name,sex,null,null,null,null,deptId,null));
    }

    @Override
    public void saveEmp(Employee employee) {
        employeeDao.insert(employee);
    }

    @Override
    public void deleteEmp(Integer empId) {
        employeeDao.deleteById(empId);
    }

    @Override
    public void updateEmp(Employee employee) {
        employeeDao.updateById(employee);
    }

    @Override
    public Employee findById(Integer empId) {
        return employeeDao.findById(empId);
    }
}
